#This is a test on the concurrency limits
import time
from random import randint
from twisted.internet import reactor, defer
from mediadart.utils import default_start_mqueue
from mediadart.mqueue.mqserver import startMQ, MQServer
from mediadart.utils import RunProc


class Pippo(Exception):
    pass

class DoStuff(MQServer):
    def mq_go(self, filename, seconds):
        if randint(0,3) == 1:
            f = open('%s.done' % filename, 'w')
            f.write('%.1f: %s failure at %s\n' % (time.time(), filename, time.asctime()))
            f.close()
            raise Pippo('FAILURE: %s' % filename)
        r = RunProc.run('./createfile.py', [filename, str(seconds)], {}, do_log=True)
        return r.getResult()

if __name__=='__main__':
    #startMQ(DoStuff, 'localhost', 5672, 'guest', 'guest')
    default_start_mqueue(DoStuff)
    reactor.run()

